import json
import re
import sys
sys.path.append("/home/sivan/0_0/projects/gitee/Small_Tools/utils")
from request import Request

class WriteNotepad:
    def __init__(self, params):
        self.qqmail_info = self.__get_conf("/home/sivan/0_0/key_info/qqmail/write_notepad.json")
        self.url = "https://mail.qq.com/cgi-bin/note_edit?"
        qi = self.qqmail_info
        qi["body"].update(params)
        for p in qi["url_params"]:
            for p2 in qi["url_params"][p]:
                self.url += f"{p}={p2}&"
        self.url = self.url[:-1]
    def __get_conf(self, file):
        with open(file) as f:
            src = f.read()
        return json.loads(src)
    def send(self, data):
        qi = self.qqmail_info
        send_data = ""
        for i in data.split("\n"):
            send_data += f"<div>{i}</div>"
        qi["body"]["content__html"] = send_data
        m = Request.post(self.url, qi["body"], qi["headers"], qi["cookie"])
        a = re.search(r'errmsg *: \"(.*?)\"', m)
        return a.group()

if __name__ == '__main__':
    params = {
        "subject": "消息通知",
        "star": "1",
        "noteid": "_3689",
    }
    a = WriteNotepad(params).send("123\n456\n789")
    print(a)


